from offer.data_structure import LinkedNode


class Task18_2:
    def run(self, head):
        before_head = LinkedNode(None)
        before_head.next = head

        node = before_head
        while node.next is not None and node.next.next is not None:
            if node.next.val != node.next.next.val:
                node = node.next
            else:
                before_duplicate = node
                node = node.next
                while node is not None and node.val == before_duplicate.next.val:
                    node = node.next
                before_duplicate.next = node
                node = before_duplicate

        return before_head.next
